﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web;
using System.Web.Http;
using StudentWeb.BLL;
using Newtonsoft.Json.Linq;
using StudentWeb.Common;

namespace StudentWeb.Api.Controllers
{
    public class ReviewsController : ApiController
    {
        ReviewsBO _ReviewsBO = new ReviewsBO();
        #region 评论信息 全部记录
        [HttpGet]
        public IHttpActionResult GetReviewsList()
        {
            try
            {
                var result = new ReviewsBO().GetReviewsList();
                return Json(result);
            }
            catch (Exception ex)
            {
                return Json(new Result(ResultCode.Fail, ex.Message));
            }
        }
        #endregion
        #region 评论信息 添加
        [HttpPost]
        public IHttpActionResult InsertReviewsInfo([FromBody]JObject obj)
        {
            dynamic model = obj;
            if (model == null) return Json(new Result(ResultCode.Fail, "缺少参数"));
            string TouristID = model.TouristID ?? "";
            string ReviewContent = model.ReviewContent ?? "";
            string ReviewDate = model.ReviewDate ?? "";
            string Rating = model.Rating ?? "";
            if (string.IsNullOrEmpty(TouristID))
            {
                return Json(new Result(ResultCode.Fail, "用户ID不能为空"));
            }
            if (string.IsNullOrEmpty(ReviewContent))
            {
                return Json(new Result(ResultCode.Fail, "评论内容不能为空"));
            }
            if (string.IsNullOrEmpty(Rating))
            {
                return Json(new Result(ResultCode.Fail, "评论等级不能为空"));
            }
            try
            {
                var result = _ReviewsBO.InsertReviewsInfo(model);
                return result ? Json(new Result(ResultCode.Success, "添加成功")) :
                Json(new Result(ResultCode.Fail, "添加失败"));
            }
            catch (Exception ex)
            {
                return Json(new Result(ResultCode.Fail, ex.Message));
            }
        }
        #endregion
        #region 评论信息 修改
        [HttpPost]
        public IHttpActionResult UpdateReviewsInfo([FromBody]JObject obj)
        {
            dynamic model = obj;
            if (model == null) return Json(new Result(ResultCode.Fail, "缺少参数"));
            int ReviewID = model.ReviewID ?? 0;
            string TouristID = model.TouristID ?? "";
            string ReviewContent = model.ReviewContent ?? "";
            string ReviewDate = model.ReviewDate ?? "";
            string Rating = model.Rating ?? "";
            if (ReviewID == 0)
            {
                return Json(new Result(ResultCode.Fail, "评论ID不能为空"));
            }
            if (string.IsNullOrEmpty(TouristID))
            {
                return Json(new Result(ResultCode.Fail, "用户ID不能为空"));
            }
            if (string.IsNullOrEmpty(ReviewContent))
            {
                return Json(new Result(ResultCode.Fail, "评论内容不能为空"));
            }
            if (string.IsNullOrEmpty(Rating))
            {
                return Json(new Result(ResultCode.Fail, "评论等级不能为空"));
            }
            try
            {
                var result = _ReviewsBO.UpdateReviewsInfo(model);
                return result ? Json(new Result(ResultCode.Success, "修改成功")) :
                Json(new Result(ResultCode.Fail, "修改失败"));
            }
            catch (Exception ex)
            {
                return Json(new Result(ResultCode.Fail, ex.Message));
            }
        }
        #endregion
        #region 评论信息 删除
        [HttpPost]
        public IHttpActionResult DeleteReviewsInfo([FromBody]JObject obj)
        {
            dynamic model = obj;
            int ReviewID = model.ReviewID ?? 0;
            if (ReviewID == 0)
            {
                return Json(new Result(ResultCode.Fail, "ID 不能为空"));
            }
            try
            {
                var result = _ReviewsBO.DeleteReviewsInfo(ReviewID);
                return Json(result);
            }
            catch (Exception ex)
            {
                return Json(new Result(ResultCode.Fail, ex.Message));
            }
        }
        #endregion
    }
}
